package com.lottery.obtainData.SscYesWinningFactory;

import com.lottery.initData.InitData;

import java.util.*;

public class SscWinResult {
    private Map<String, List<Integer>> opencodeMap = new HashMap<>();

    //通过开奖号码初始化ssc中奖结果集
    public void init(String code) {
        InitData.SSC_WINRESULT_MAP.clear();
        String[] codes = code.split(",");
        List<Integer> list = new ArrayList<>();
        for (String ss : codes) {
            int aa = Integer.parseInt(ss);
            list.add(aa);
        }
        daxiao(list);
        shuzi(list);
        tema(list);
        danma(codes);
        longhu(list);
        sanqiu(list);
    }

    //三球组开奖结果集
    private void sanqiu(List<Integer> code) {
        opencodeMap.clear();
        Integer playwayId = 9;

        List<String> strings = new ArrayList<>();
        List<Integer> list = null;
        for (int i = 0; i < 3; i++) {
            list = new ArrayList<>();
            list.add(code.get(i));
            list.add(code.get(i + 1));
            list.add(code.get(i + 2));
            String aa = "";
            switch (i) {
                case 0:
                    aa = "前";
                    break;
                case 1:
                    aa = "中";
                    break;
                case 2:
                    aa = "后";
                    break;
            }
            opencodeMap.put(aa, list);
        }
        getSscShunzi(strings);
        getSscBaozi(strings);
        getSscDuizi(strings);
        getSscBanShun(strings);
        getZaLiu(strings);
        InitData.SSC_WINRESULT_MAP.put(playwayId, strings);
    }

    //杂六开奖结果集
    private void getZaLiu(List<String> strings) {
        opencodeMap.forEach((k, v) -> {
            boolean flag = true;
            String[] a = new String[]{k+"/豹子",k+"/顺子",k+"/半顺",k+"/对子"};
            for(String ss :a){
                if(strings.contains(ss)){
                    flag = false;
                };
            }
            if(flag){
                strings.add(k+"/杂六");
            }
        });

    }



    //猜龙虎开奖结果集
    private void longhu(List<Integer> list) {
        Integer playwayId = 8;
        List<String> strings = new ArrayList<>();
        int first = list.get(0);
        int end = list.get(4);
        if (first > end) {
            strings.add("龙");
        } else if (first == end) {
            strings.add("合");
        } else {
            strings.add("虎");
        }
        InitData.SSC_WINRESULT_MAP.put(playwayId, strings);
    }

    //单码开奖集合
    private void danma(String[] codes) {
        Integer playwayId = 7;
        List<String> strings = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        for (String aa : codes) {
            if (!map.containsKey(aa)) {
                map.put(aa, 1);
            } else {
                map.put(aa, map.get(aa) + 1);
            }
        }
        map.forEach((k, v) -> {
            strings.add(k + "/" + v);//前面是开奖号码,后面的数字用于拉取赔率
        });
        InitData.SSC_WINRESULT_MAP.put(playwayId, strings);
    }

    //特码开奖集合
    private void tema(List<Integer> codes) {
        Integer playwayId = 6;
        List<String> strings = new ArrayList<>();
        int count = 0;
        for (Integer i : codes) {
            count += i;
        }
        if (count > 22) {
            strings.add("总/大");
            if (count % 2 == 0) {
                strings.add("总/大双");
                strings.add("总/双");
            } else {
                strings.add("总/大单");
                strings.add("总/单");
            }
        } else {
            strings.add("总/小");
            if (count % 2 == 0) {
                strings.add("总/小双");
                strings.add("总/双");
            } else {
                strings.add("总/小单");
                strings.add("总/单");
            }
        }
        if (count < 16) {
            strings.add("A");
        } else if (15 < count && count < 30) {
            strings.add("B");
        } else {
            strings.add("C");
        }
        InitData.SSC_WINRESULT_MAP.put(playwayId, strings);
    }

    //数字开奖集合
    private void shuzi(List<Integer> codes) {
        Integer playwayId = 5;
        List<String> strings = new ArrayList<>();
        for (int i = 1; i <= codes.size(); i++) {
            Integer a = codes.get(i - 1);
            strings.add(i + "/" + a);
        }
        InitData.SSC_WINRESULT_MAP.put(playwayId, strings);
    }

    //大小的中奖集合
    private void daxiao(List<Integer> codes) {
        Integer playwayId = 4;
        List<String> strings = new ArrayList<>();
        for (int i = 1; i <= codes.size(); i++) {
            Integer a = codes.get(i - 1);
            if (a > 4) {
                strings.add(i + "/大");
            } else {
                strings.add(i + "/小");
            }
            if (a % 2 == 0) {
                strings.add(i + "/双");
            } else {
                strings.add(i + "/单");
            }
        }
        InitData.SSC_WINRESULT_MAP.put(playwayId, strings);
    }


    //获取顺子中奖的情况
    private void getSscShunzi(List<String> strings) {
        opencodeMap.forEach((k, v) -> {
            int[] opencodeToInt = new int[]{
                    v.get(0), v.get(1), v.get(2)
            };
            Arrays.sort(opencodeToInt);
            String special = opencodeToInt[0] + "" + opencodeToInt[1] + "" + opencodeToInt[2];
            List<String> list = new ArrayList<>();
            list.add("019");
            list.add("089");
            boolean common = (opencodeToInt[1] - opencodeToInt[0] == 1) && (opencodeToInt[2] - opencodeToInt[1] == 1);
            if (common || list.contains(special)) {
                strings.add(k + "/顺子");
            }
        });
    }

    //获取豹子中奖的情况
    private void getSscBaozi(List<String> strings) {
        opencodeMap.forEach((k, v) -> {
            List<Integer> list = v;
            if ((list.get(0) == list.get(1)) && (list.get(0) == list.get(2))) {
                strings.add(k + "/豹子");
            }
        });
    }

    //获取对子中奖的情况
    private void getSscDuizi(List<String> strings) {
        opencodeMap.forEach((k, v) -> {
            List<Integer> list = v;
            Set<Integer> set = new HashSet<>();
            for (Integer string : list) {
                try {
                    set.add(string);
                } catch (Exception e) {
                }
            }
            if (set.size() == 2) {
                strings.add(k + "/对子");
            }
        });
    }

    //获取半顺的中奖情况
    private void getSscBanShun(List<String> strings) {
        opencodeMap.forEach((k, v) -> {
            List<Integer> list = v;
            //排除顺子的情况
            int[] opencodeToInt = new int[]{
                    list.get(0), list.get(1), list.get(2)
            };
            Arrays.sort(opencodeToInt);
            String special = opencodeToInt[0] + "" + opencodeToInt[1] + "" + opencodeToInt[2];
            List<String> listzz = new ArrayList<>();
            listzz.add("019");
            listzz.add("089");
            boolean common = (opencodeToInt[1] - opencodeToInt[0] == 1) && (opencodeToInt[2] - opencodeToInt[1] == 1);
            boolean shunzi = (common || listzz.contains(special));
            boolean duizi = strings.contains(k + "/对子");
            boolean other = v.contains(9)&& v.contains(0);
            if ((!shunzi) && (!duizi)) {
                if (Math.abs(opencodeToInt[0] - opencodeToInt[1]) == 1 || Math.abs(opencodeToInt[1] - opencodeToInt[2]) == 1 || other) {
                    strings.add(k + "/半顺");
                }
            }
        });
    }

}
